package com.shop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.entity.shop.Category;
import com.shop.entity.shop.CategorySecond;
import com.shop.entity.shop.Product;
import com.shop.service.shop.Product_service;

@Controller
public class PageJump {
	
	@Autowired
	private Product_service productSer;

	// 注册页面
	@RequestMapping("/toRegister")
	public String toRegister(HttpServletRequest request) {
		Map<String, Boolean> verifyM = new HashMap<String, Boolean>();
		verifyM.put("username", false);
		verifyM.put("password1", false);
		verifyM.put("password2", false);
		verifyM.put("email", false);
		verifyM.put("verifyCode", false);
		request.getSession().setAttribute("registerVerify", verifyM);
		return "register";
	}

	// 到登录页面
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}

	// 到主页面
	@RequestMapping("/toIndex")
	public String toIndex() {
		return "index";
	}

	// 到商品分类
	@RequestMapping("/toProductList")
	public String toProductList(HttpServletRequest request) {
		//1.查询一级分类（List进行封装），以及所对应的二级分类（List）
		List<Category> categorys = productSer.queayCategorys();
		request.setAttribute("categoryList", categorys);
		//2.得到所有商品（List进行封装）
		List<Product> products = productSer.qreayProducts();
		request.setAttribute("productList", products);
		return "productList";
	}
	
	// 到商品一级分类
	@RequestMapping("/toCategory")
	public String toCategory(HttpServletRequest request){
		String categoryIdToString = request.getParameter("categoryId");
		int categoryId = Integer.parseInt(categoryIdToString);
		List<CategorySecond> categorySecondList = productSer.queayCategorySecondByCid(categoryId);
		List<Product> productList = new ArrayList<>();
		for (CategorySecond cs : categorySecondList) {
			List<Product> products = productSer.qreayProductByCsid(cs.getCsid());
			productList.addAll(products);
		}
		request.setAttribute("productList", productList);
		return "pagejump/queryProduct";
	}
	
	// 到商品二级分类
	@RequestMapping("/toCategorySecond")
	public String toCategorySecond(HttpServletRequest request){
		String csIdToString = request.getParameter("categorySecondId");
		int csId = Integer.parseInt(csIdToString);
		List<Product> productList = productSer.qreayProductByCsid(csId);
		request.setAttribute("productList", productList);
		return "pagejump/queryProduct";
	}
	
	//到个人中心
	@RequestMapping("/toUser")
	public String toUser(){
		
		return "user";
	}
	
	//退出登陆
	@RequestMapping("/toRegisterEsc")
	public String toRegisterEsc(HttpServletRequest request){
//		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return "index";
	}
	
	
}
